import 'package:flutter/material.dart';
import 'package:flutter_form_builder/flutter_form_builder.dart';
import 'package:fucius/Models/talentModel.dart';
import 'package:fucius/utils/firestoreservice.dart';
import 'package:fucius/widgets/login/authentication.dart';



//class EditInfo extends StatelessWidget {
//  @override
//  Widget build(BuildContext context) {
//    return MaterialApp(
//      theme: ThemeData(
//        primarySwatch: Colors.blue,
//      ),
//      home: EditInfoPage(),
//    );
//  }
//}

class EditInfoPage extends StatefulWidget {
  EditInfoPage({Key key, this.auth, this.userId,
    this.first_name, this.middle_name, this.last_name, this.name, this.job_title, this.country, this.major,
    this.organization, this.phone, this.email, this.person_website, this.short_bio,
    this.logoutCallback})
      : super(key: key);


  String first_name = "";
  String middle_name = "";
  String last_name = "";
  String name = "";
  String job_title = "";
  String country = "";
  String major = "";
  String organization = "";
  String phone = "";
  String email = "";
  String person_website = "";
  String short_bio = "";

  final BaseAuth auth;
  final VoidCallback logoutCallback;
  final String userId;
  @override
  EditInfoPageState createState() {
    return EditInfoPageState();
  }
}

class EditInfoPageState extends State<EditInfoPage> {
  var data;
  bool autoValidate = true;
  bool readOnly = false;
  bool showSegmentedControl = true;
  final GlobalKey<FormBuilderState> _fbKey = GlobalKey<FormBuilderState>();
  final GlobalKey<FormFieldState> _specifyTextFieldKey =
  GlobalKey<FormFieldState>();
  TalentModel talent;
  FirestoreService fireServ = new FirestoreService();

  ValueChanged _onChanged = (val) => print(val);
  var genderOptions = ["Afghanistan","Åland Islands","Albania","Algeria","American Samoa","Andorra","Angola","Anguilla","Antarctica","Antigua and Barbuda","Argentina","Armenia","Aruba","Australia","Austria","Azerbaijan","Bahamas","Bahrain","Bangladesh","Barbados","Belarus","Belgium","Belize","Benin","Bermuda","Bhutan","Bolivia, Plurinational State of","Bonaire, Sint Eustatius and Saba","Bosnia and Herzegovina","Botswana","Bouvet Island","Brazil","British Indian Ocean Territory","Brunei Darussalam","Bulgaria","Burkina Faso","Burundi","Cambodia","Cameroon","Canada","Cape Verde","Cayman Islands","Central African Republic","Chad","Chile","China","Christmas Island","Cocos (Keeling) Islands","Colombia","Comoros","Congo","Congo, the Democratic Republic of the","Cook Islands","Costa Rica","Côte d'Ivoire","Croatia","Cuba","Curaçao","Cyprus","Czech Republic","Denmark","Djibouti","Dominica","Dominican Republic","Ecuador","Egypt","El Salvador","Equatorial Guinea","Eritrea","Estonia","Ethiopia","Falkland Islands (Malvinas)","Faroe Islands","Fiji","Finland","France","French Guiana","French Polynesia","French Southern Territories","Gabon","Gambia","Georgia","Germany","Ghana","Gibraltar","Greece","Greenland","Grenada","Guadeloupe","Guam","Guatemala","Guernsey","Guinea","Guinea-Bissau","Guyana","Haiti","Heard Island and McDonald Islands","Holy See (Vatican City State)","Honduras","Hong Kong","Hungary","Iceland","India","Indonesia","Iran, Islamic Republic of","Iraq","Ireland","Isle of Man","Israel","Italy","Jamaica","Japan","Jersey","Jordan","Kazakhstan","Kenya","Kiribati","Korea, Democratic People's Republic of","Korea, Republic of","Kuwait","Kyrgyzstan","Lao People's Democratic Republic","Latvia","Lebanon","Lesotho","Liberia","Libya","Liechtenstein","Lithuania","Luxembourg","Macao","Macedonia, the Former Yugoslav Republic of","Madagascar","Malawi","Malaysia","Maldives","Mali","Malta","Marshall Islands","Martinique","Mauritania","Mauritius","Mayotte","Mexico","Micronesia, Federated States of","Moldova, Republic of","Monaco","Mongolia","Montenegro","Montserrat","Morocco","Mozambique","Myanmar","Namibia","Nauru","Nepal","Netherlands","New Caledonia","New Zealand","Nicaragua","Niger","Nigeria","Niue","Norfolk Island","Northern Mariana Islands","Norway","Oman","Pakistan","Palau","Palestine, State of","Panama","Papua New Guinea","Paraguay","Peru","Philippines","Pitcairn","Poland","Portugal","Puerto Rico","Qatar","Réunion","Romania","Russian Federation","Rwanda","Saint Barthélemy","Saint Helena, Ascension and Tristan da Cunha","Saint Kitts and Nevis","Saint Lucia","Saint Martin (French part)","Saint Pierre and Miquelon","Saint Vincent and the Grenadines","Samoa","San Marino","Sao Tome and Principe","Saudi Arabia","Senegal","Serbia","Seychelles","Sierra Leone","Singapore","Sint Maarten (Dutch part)","Slovakia","Slovenia","Solomon Islands","Somalia","South Africa","South Georgia and the South Sandwich Islands","South Sudan","Spain","Sri Lanka","Sudan","Suriname","Svalbard and Jan Mayen","Swaziland","Sweden","Switzerland","Syrian Arab Republic","Taiwan, Province of China","Tajikistan","Tanzania, United Republic of","Thailand","Timor-Leste","Togo","Tokelau","Tonga","Trinidad and Tobago","Tunisia","Turkey","Turkmenistan","Turks and Caicos Islands","Tuvalu","Uganda","Ukraine","United Arab Emirates","United Kingdom","United States","United States Minor Outlying Islands","Uruguay","Uzbekistan","Vanuatu","Venezuela, Bolivarian Republic of","Viet Nam","Virgin Islands, British","Virgin Islands, U.S.","Wallis and Futuna","Western Sahara","Yemen","Zambia","Zimba",];

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(""),
      ),
      body: Padding(
        padding: EdgeInsets.all(10),
        child: SingleChildScrollView(
          child: Column(
            children: <Widget>[
              FormBuilder(
                // context,
                key: _fbKey,
//                autovalidate: true,
                initialValue: {
                  'id': widget.userId,
                  'First Name': widget.first_name,
                  'Middle Name': widget.middle_name,
                  'Last Name': widget.last_name,
                  'Name': widget.name,
                  'Job Title': widget.job_title,
//                  'Country': widget.country,
                  'Major': widget.major,
                  'Organization': widget.organization,
                  'Phone': widget.phone,
                  'Email': widget.email,
                  'Personal Website': widget.person_website,
                  'Short Bio': widget.short_bio,
                },
                readOnly: false,
                child: Column(
                  children: <Widget>[
                    Row(
                      children: <Widget>[
                        Expanded(child: FormBuilderTextField(
                          decoration: InputDecoration(
                            labelText: "First Name",
                          ),
                          attribute: 'First Name',
                          onChanged: _onChanged,
                        )),
                        Expanded(child: FormBuilderTextField(
                          decoration: InputDecoration(
                            labelText: "Middle Name",
                          ),
                          attribute: 'Middle Name',
                          onChanged: _onChanged,
                        )),
                        Expanded(child: FormBuilderTextField(
                          decoration: InputDecoration(
                            labelText: "Last Name",
                          ),
                          attribute: 'Last Name',
                          onChanged: _onChanged,
                        )),
                      ],
                    ),
//                    FormBuilderTextField(
//                      decoration: InputDecoration(
//                        labelText: "Name",
//                      ),
//                      attribute: 'Name',
//                      onChanged: _onChanged,
//                    ),
//                    FormBuilderTextField(
//                      decoration: InputDecoration(
//                        labelText: "Job Title",
//                      ),
//                      attribute: 'Job Title',
//                      onChanged: _onChanged,
////                      onSaved: (value) => talent.major = value.trim(),
//                    ),
                    FormBuilderDropdown(
                      attribute: "Country",
                      decoration: InputDecoration(
                        labelText: "Country",
                      ),
                      hint: Text('Select Country'),
//                      validators: [FormBuilderValidators.required()],
                      items: genderOptions
                          .map((country) => DropdownMenuItem(
                        value: country,
                        child: Text('$country'),
                      ))
                          .toList(),
//                      onSaved: (value) => talent.location = value.trim(),
                    ),
                    FormBuilderTextField(
                      decoration: InputDecoration(
                        labelText: "Major",
                      ),
                      attribute: 'Major',
                      onChanged: _onChanged,
//                      onSaved: (value) => talent.major = value.trim(),
                    ),
                    FormBuilderTextField(
                      attribute: "Organization",
                      decoration: InputDecoration(
                        labelText: "Organization",
                      ),
                      onChanged: _onChanged,
//                      valueTransformer: (text) => num.tryParse(text),
//                      keyboardType: TextInputType.number,
//                      onSaved: (value) => talent.age = value.trim(),
                    ),
                    FormBuilderTextField(
                      attribute: "Phone",
                      decoration: InputDecoration(
                        labelText: "Phone",
                      ),
                      onChanged: _onChanged,
//                      valueTransformer: (text) => num.tryParse(text),
//                      keyboardType: TextInputType.number,
                    ),
                    FormBuilderTextField(
                      decoration: InputDecoration(
                        labelText: "Email",
                      ),
                      attribute: 'Email',
                      onChanged: _onChanged,
//                      onSaved: (value) => talent.location = value.trim(),
                    ),
                    FormBuilderTextField(
                      decoration: InputDecoration(
                        labelText: "Personal Website",
                      ),
                      attribute: 'Personal Website',
                      onChanged: _onChanged,
//                      onSaved: (value) => talent.resume = value.trim(),
                    ),
                    FormBuilderTextField(
                      maxLines: 8,
                      decoration: InputDecoration(
                        labelText: "Short Bio",
                      ),
                      attribute: 'Short Bio',
                      onChanged: _onChanged,
//                      onSaved: (value) => talent.resume = value.trim(),
                    ),

                  ],
                ),
              ),

              Row(
                children: <Widget>[
                  Expanded(
                    child: MaterialButton(
                      color: Theme.of(context).accentColor,
                      child: Text(
                        "Submit",
                        style: TextStyle(color: Colors.white),
                      ),
                      onPressed: () {
                        if (_fbKey.currentState.saveAndValidate()) {
                          data = _fbKey.currentState.value;
                          print(data);
                          fireServ.createTalent(widget.userId, data);
                          Navigator.pop(context);
                        } else {
                          print(_fbKey.currentState.value);
                          print("validation failed");
                        }
                      },
                    ),
                  ),
                  SizedBox(
                    width: 20,
                  ),
                  Expanded(
                    child: MaterialButton(
                      color: Theme.of(context).accentColor,
                      child: Text(
                        "Reset",
                        style: TextStyle(color: Colors.white),
                      ),
                      onPressed: () {
                        _fbKey.currentState.reset();
                      },
                    ),
                  ),
                ],
              ),
            ],
          ),
        ),
      ),
    );
  }
}